<?php 

// +----------------------------------------------------------------------
// | 文件 STS.class.php
// +----------------------------------------------------------------------
// | 说明 阿里云STS授权管理
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

namespace Common\Util\aliyun;
include_once 'aliyun-php-sdk-core/Config.php';
include_once 'aliyun-php-sdk-core/Profile/DefaultProfile.php';
include_once 'aliyun-php-sdk-core/DefaultAcsClient.php';
use Sts\Request\V20150401 as OssSts;

class STS
{

	protected $client;
	
	public function __construct()
	{
		$iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "GdOigjX4FEhcYh67", "vBn8AAkBwwMFXhXJhlcSYcSD6j1L5O");
		$this->client = new \DefaultAcsClient($iClientProfile);
	}

	public function getRole($key)
	{
			$roleArn = "acs:ram::1766781728755868:role/ly-66tv";
// 在扮演角色(AssumeRole)时，可以附加一个授权策略，进一步限制角色的权限；
// 详情请参考《RAM使用指南》
// 此授权策略表示读取所有OSS的只读权限
$policy=<<<POLICY
{
  "Statement": [
    {
      "Action": [
        "oss:Put*",
        "oss:Get*",
        "oss:List*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ],
  "Version": "1"
}
POLICY;

			$request = new OssSts\AssumeRoleRequest();
			// RoleSessionName即临时身份的会话名称，用于区分不同的临时身份
			// 您可以使用您的客户的ID作为会话名称
			$request->setRoleSessionName("66tv-app.{$key}");
			$request->setRoleArn($roleArn);
			$request->setPolicy($policy);
			$request->setDurationSeconds(3600);
			try {
				$response = $this->client->doAction($request);
			} catch (\Exception $e) {
				return null;
			}
			
			if ( $response->getStatus() != 200){
				return null;
			}
			return $response->getBody();
		
	}

}